var DomListener = {
  //省份城市联动，只需要在省份的select加上.relation_province_selector,并且在city的select加上.relation_city_selector
  //一个页面可以有多个省份城市联动，不过目前最大支持两层，即：
  //  div province
  //  div city
  // 或者 province跟city下拉是相邻的
  bind_province_city_selector: function () {
    $('.relation_province_selector').unbind().change(function () {
      var city_obj = $(this).siblings('.relation_city_selector:first') || $(this).parent().parent().find('.relation_city_selector')
      if (city_obj.length == 0) return;
      if (is_blank($(this).val())) {
        city_obj.html('')
      } else {
        $.ajax({
          url: '/data_api/get_cities',
          dataType: 'json',
          type: 'put',
          data: {id: $(this).val()},
          success: function (r) {
            var options = '<option></option>' + $.map(r, function (e) {
                  return "<option value='" + e[1] + "'>" + e[0] + "</option>"
                }).join()
            city_obj.html(options)
          }

        })
      }
    })
  },
  // 通过点击panel里面的+/-，展开收起panel-body
  bind_expand_panel: function () {
    $(document).undelegate('i.expand-panel-body', 'click').delegate('i.expand-panel-body', 'click', function () {
      if ($(this).hasClass('fa-minus-square-o')) {
        $(this).removeClass('fa-minus-square-o').addClass('fa-plus-square-o').parents('.panel:first').find('.panel-body').addClass('hide')
      } else {
        $(this).removeClass('fa-plus-square-o').addClass('fa-minus-square-o').parents('.panel:first').find('.panel-body').removeClass('hide')
      }
    })
  },

  // 绑定modal弹框ajax加载的datepicker
  // 不用在init方法里加上此方法，因为在app.js里面已经初始化过一遍
  // 在input里格式化结果 --> data: {'date-format': 'Y-M-D H:m'}
  bind_modal_datetimepicker: function(){
    $('#application-modal .js-datetimepicker').each(function(){
      var $input = $(this)
      $input.datetimepicker({
        format: $input.data('format') ? $input.data('format') : false,
        useCurrent: $input.data('use-current') ? $input.data('use-current') : false,
        locale: moment.locale('' + ($input.data('locale') ? $input.data('locale') : '') +''),
        showTodayButton: $input.data('show-today-button') ? $input.data('show-today-button') : false,
        showClear: $input.data('show-clear') ? $input.data('show-clear') : false,
        showClose: $input.data('show-close') ? $input.data('show-close') : false,
        sideBySide: $input.data('side-by-side') ? $input.data('side-by-side') : false,
        inline: $input.data('inline') ? $input.data('inline') : false,
        icons: {
          time: 'si si-clock',
          date: 'si si-calendar',
          up: 'si si-arrow-up',
          down: 'si si-arrow-down',
          previous: 'si si-arrow-left',
          next: 'si si-arrow-right',
          today: 'si si-size-actual',
          clear: 'si si-trash',
          close: 'si si-close'
        }
      });
    })

  },

  // 修复css-checkbox点击问题
  bind_modal_css_checkbox: function(){
    $('#application-modal .css-checkbox').unbind().click(function(){$(this).find('input:checkbox').click()})
    // $('#application-modal .css-checkbox>input:checkbox')
  },

  bind_reset_button: function(){
    $("a[type=reset]").click(function () {
      $(this).parents('form:first').find(':input').not(':button, :submit, :reset, :hidden').val('');
      return false;
    });
  },
  set_ajax_header: function(){
  // Ajax提交表单的时候加上token，防止 InvalidAuthenticityToken 报错
    $.ajaxSetup({
      beforeSend: function(xhr) {
        xhr.setRequestHeader("X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content"));
      }
    })
  },
  init: function () {
    this.bind_province_city_selector()
    this.bind_expand_panel()
    this.bind_reset_button()
    this.set_ajax_header()
  }
}
